// pages/shopdetail/index.js
import api from '../../request/api.js'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 轮播图数组
    swiperList: [],
    current:1,
    id : '',
    text:'',
    credit:0,
    goods_name:'',
    btnDown : false,
    type : '',
    address_id : ''
  },
  currentChange(e){
    this.setData({
      current: e.detail.current+1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.data.id = options.id;
  },
  goConvert(){
    var that = this;
    that.setData({
      btnDown : true
    })
    wx.navigateTo({
      url: '../address/index?type=shop',
    })
  },
  async convert(){
    var that = this;
    const { data: res } = await api('POST', 'Recycle/goods/exchangeGoods',{
      token: wx.getStorageSync('token'),
      goods_id:that.data.id,
      address_id:this.data.address_id
    })
    if(res.code == 200){
      Toast(res.msg); 
      setTimeout(function () {
        wx.switchTab({
          url: '../mall/index',
        })
       }, 1500) 
    }else{
      Toast(res.msg)
    }
  },
  async getDetail(id){
    var that = this;
    const { data: res } = await api('GET', 'Recycle/goods/getGoodsInfo',{
      token: wx.getStorageSync('token'),
      goods_id:id
    })
    if(res.code ===200){
      this.setData({
        swiperList: that.listArray(res.data.goods_images),
        text:res.data.introduction,
        credit:res.data.credit,
        name:res.data.goods_name
      })
    }
  },
  // 图片数据处理
  listArray(data){
    var that = this;
    let list = that.data.swiperList;
    for (let i = 0; i < data.length; i++) {
      list.push({
        image_src :  app.globalData.imageUrl+data[i],
      })
    };
    return list;  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.type == 'address'){
      this.convert();
    }
    this.getDetail(this.data.id);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})